package DepthFirstSearch;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class _841_KeysandRooms {
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        if (rooms == null || rooms.size() == 0) {
            return false;
        }
        Queue<Integer> queue = new LinkedList<Integer>();
        boolean[] visited = new boolean[rooms.size()];
        List<Integer> start = rooms.get(0);
        visited[0] = true;
        for (int i : start) {
            queue.add(i);
        }
        while (!queue.isEmpty()) {
            visited[queue.peek()] = true;
            List<Integer> temp = rooms.get(queue.poll());
            for (int i : temp) {
                if (!visited[i]) {
                    queue.add(i);
                }
            }
        }
        //check every room if has been visited
        for (boolean visit : visited) {
            if (!visit) {
                return false;
            }
        }
        return true;
    }
}
